package com.blt.car.me;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseCommonAdapter;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.IBaseAdapter;
import com.blt.car.base.ViewHolder;
import com.blt.car.me.adapter.BillJourneyAdapter;
import com.blt.car.utils.ActivityHelper;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.journey.JourneyApi;
import blt.app_core.api.journey.bean.Travel;
import blt.app_core.api.journey.bean.TravelOrder;
import blt.app_core.api.other.OutApi;

/**
 * Created by xgp on 2017/6/20.
 */

public class ChooseTripsActivity extends BaseTitleBarActivity {
    private ListView lvChooseTrips;
    private BillJourneyAdapter mAdapter;

    private TextView nums;
    private TextView totalAmount;

    private int invoiceAmount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_trips;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(JourneyApi.class).getTravel(1, 10,true,false);
    }

    boolean select;

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("选择行程");
        mTitleBar.setRightText("全选");
        mTitleBar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select) {
                    select = false;
                } else {
                    select = true;
                }
                mAdapter.selectAll(select);

            }
        });

        totalAmount = holder.get(R.id.chose_trip_total_amount);
        nums = holder.get(R.id.chose_trip_chose_nums);

        lvChooseTrips = holder.get(R.id.lv_choose_trips);
        mAdapter = new BillJourneyAdapter(this, R.layout.item_choose_trips);
        lvChooseTrips.setAdapter(mAdapter);
    }

    public void doNextApplyBill(View view) {
        ActivityHelper.toApplyBill(this, mAdapter.getSelectItem(),invoiceAmount);
    }


    @CoreEvent(coreClientClass = JourneyApi.class)
    public void onTravel(Travel travel) {
        mAdapter.addData(travel.travelOrderList);
        mAdapter.init();
    }


    @CoreEvent(coreClientClass = OutApi.class)
    public void onSelect() {
        List<TravelOrder> orders = mAdapter.getSelectCount();
        nums.setText(orders.size() + "个行程");
        int total = 0;
        for (TravelOrder order : orders) {
            total += order.amount;
        }
        invoiceAmount = total;
        totalAmount.setText(total + "元");
    }
}
